package com.cuit.dao.impl;

import com.cuit.dao.PatientDao;
import com.cuit.pojo.AcceptOrderData;
import com.cuit.pojo.Doctor;
import com.cuit.pojo.Patient;

import java.util.List;

public class PatientDaoImpl extends BaseDao implements PatientDao {
    @Override
    public Patient queryById(String p_id) {
        String sql="select * from patient where p_id=?";
        return queryForOne(Patient.class,sql,p_id);
    }

    @Override
    public Patient queryByIdAndPassword(String p_id, String p_password) {
        String sql="select * from patient where p_id=? and p_password=?";
        return queryForOne(Patient.class,sql,p_id,p_password);
    }

    @Override
    public int save(Patient patient) {
        String sql="insert into patient(`p_id`,`p_name`,`p_password`,`id_card`,`p_sex`,`p_age`) values(?,?,?,?,?,?)";
        return update(sql,patient.getP_id(),patient.getP_name(),patient.getP_password(),patient.getId_card(),patient.getP_sex(),patient.getP_age());
    }

    @Override
    public List<AcceptOrderData> queryDoctor(String doctor_name, String department, String category,String[] sitting_time,int max_order, int[] d_week) {
        System.out.println(doctor_name+" "+department+" "+category+" "+sitting_time[0]+" "+max_order+" "+d_week[0]);
        if(doctor_name!=""&&max_order!=0){
            String sql="select `d_name`," +
                    "d_order.`d_id`," +
                    "`d_age`," +
                    "`d_sex`," +
                    "`d_grade`," +
                    "`d_department`," +
                    "`d_sitting_time` as `sitting_time`," +
                    "`d_week` "+
                    "from `doctors`,`d_order` where `d_name`=? and `d_department`=? and `d_grade`=? and `max_order`=? and d_order.`d_week`=? and `d_sitting_time`=? and doctors.`d_id`=d_order.`d_id`"+
                    "order by d_order.`d_id` asc ";
            return queryForList(AcceptOrderData.class,sql,doctor_name,department,category,max_order,d_week[0],sitting_time[0]);
        }else if(doctor_name!=""&&max_order==0){
            String sql="select `d_name`," +
                    "d_order.`d_id`," +
                    "`d_age`," +
                    "`d_sex`," +
                    "`d_grade`," +
                    "`d_department`," +
                    "`d_sitting_time` as `sitting_time`," +
                    "`d_week` "+
                    "from `doctors`,`d_order` where `d_name`=? and doctors.`d_department`=? and doctors.`d_grade`=? and d_order.`d_week`=? and `d_sitting_time`=? and doctors.`d_id`=d_order.`d_id`"+
                    "order by d_order.`d_id` asc ";
            return queryForList(AcceptOrderData.class,sql,doctor_name,department,category,d_week[0],sitting_time[0]);
        }else if(doctor_name==""&&max_order!=0){
            String sql="select `d_name`," +
                    "d_order.`d_id`," +
                    "`d_age`," +
                    "`d_sex`," +
                    "`d_grade`," +
                    "`d_department`," +
                    "`d_sitting_time` as `sitting_time`," +
                    "`d_week` "+
                    "from `doctors`,`d_order` where doctors.`d_department`=? and doctors.`d_grade`=? and d_order.`d_week`=? and `max_order`=? and `d_sitting_time`=? and doctors.`d_id`=d_order.`d_id`"+
                    "order by d_order.`d_id` asc ";
            return queryForList(AcceptOrderData.class,sql,department,category,d_week[0],max_order,sitting_time[0]);
        }else{
            String sql="select `d_name`," +
                    "d_order.`d_id`," +
                    "`d_age`," +
                    "`d_sex`," +
                    "`d_grade`," +
                    "`d_department`," +
                    "`d_sitting_time` as `sitting_time`," +
                    "`d_week` "+
                    "from `doctors`,`d_order` where doctors.`d_department`=? and doctors.`d_grade`=? and d_order.`d_week`=? and `d_sitting_time`=? and doctors.`d_id`=d_order.`d_id`"+
                    "order by d_order.`d_id` asc ";
            return queryForList(AcceptOrderData.class,sql,department,category,d_week[0],sitting_time[0]);
        }
    }

    @Override
    public int modifyPassword(String p_id, String p_password) {
        System.out.println(p_id+" "+p_password);
        String sql="update patient set `p_password`=? where `p_id`=?";
        return update(sql,p_password,p_id);
    }

    @Override
    public int modify(Patient patient) {
        String sql="update patient set `p_name`=?,`id_card`=?,`p_age`=?,`p_sex`=? where `p_id`=?";
        return update(sql,patient.getP_name(),patient.getId_card(),patient.getP_age(),patient.getP_sex(),patient.getP_id());
    }
}
